package com.abu.server.controller;


import com.abu.server.pojo.FileEntity;
import com.abu.server.service.FileService;
import com.abu.server.utils.Resp;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-04-06
 */
@RestController
@RequestMapping("/server/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("upload")
    public Resp uploadFile(@RequestParam("file") MultipartFile file) {
        String uuid = this.fileService.uploadFile(file);
        if (!StringUtils.isBlank(uuid)) {
            return Resp.ok().data("uuid", uuid);
        }
        return Resp.error().code(500).msg("服务器上传出错");
    }

    @GetMapping("file_stream")
    public Resp uploadFile(@RequestParam("uuid") String uuid) {
        String content = this.fileService.getFileStream(uuid);
        if (!StringUtils.isBlank(content)) {
            return Resp.ok().data("content", content);
        }
        return Resp.error().code(410);
    }

    @GetMapping("meta/{uuid}")
    public Resp getMeta(@PathVariable("uuid") String uuid) {
        FileEntity fileEntity = this.fileService.getMeta(uuid);
        if (fileEntity != null) {
            return Resp.ok().data("fileEntity", fileEntity);
        }
        return Resp.error().code(404);
    }
}

